package com.chookss.mine.system;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.chookss.tools.NavigationTools;
import com.google.gson.reflect.TypeToken;
import com.chookss.R;
import com.chookss.Urls;
import com.chookss.mine.entity.PraiseEntity;
import com.chookss.tools.MyToast;
import com.chookss.tools.SignUtils2;
import com.chookss.tools.okgoconfig.MyHttpRequest;
import com.chookss.tools.okgoconfig.callback.ObserverCallback;
import com.chookss.tools.okgoconfig.model.LzyResponse;
import com.chookss.tools.Utils;
import com.chookss.view.RecycleViewDivider;
import com.lvgroup.hospital.base.BaseAct;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.RefreshState;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.annotations.NonNull;

//积分
public class MyIntegralActivity extends BaseAct {

    @BindView(R.id.common_title_txt)
    TextView tvTitle;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.smrRf)
    SmartRefreshLayout smrRf;
    @BindView(R.id.llNone)
    LinearLayout llNone;
    @BindView(R.id.tvEmpty)
    TextView tvEmpty;
    private int currentPage = 1;
    private String pageSize = "20";
    private List<PraiseEntity> list = new ArrayList<>();
    private MyIntegralAdapter integralAdapter;
    private boolean isEnd = false;//是否到底
    private int messageType = 0;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_integral);
        messageType = getIntent().getIntExtra("messageType", 0);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        initState();
        recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        recyclerView.addItemDecoration(new RecycleViewDivider(
                this, LinearLayoutManager.HORIZONTAL, Utils.dip2px(this, 0.5f), getResources().getColor(R.color.line_gray)
        ));
        integralAdapter = new MyIntegralAdapter(R.layout.item_my_integral, this, list);
        recyclerView.setAdapter(integralAdapter);
        if (messageType == 0) {
            tvTitle.setText("积分变更");
            tvEmpty.setText("暂无积分变更");
        } else {
            tvTitle.setText("系统消息");
            tvEmpty.setText("暂无系统消息");
        }

        smrRf.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                if (isEnd) {//has next page
                    MyToast.show("没有更多数据了");
                    loadEnd();
                } else {
                    currentPage++;
                    getData();
                }
            }
        });
        smrRf.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                currentPage = 1;
                isEnd = false;
                getData();
            }
        });
        smrRf.autoRefresh();
        NavigationTools.setWaterMark(this, "19");
    }

    private void getData() {
        Map<String, String> map = new HashMap<>();
//     (0:积分变更通知，1：粉丝关注通知，2：点赞通知，3：评论通知，4：系统消息通知
        map.put("messageType", messageType + "");
        map.put("pageNum", currentPage + "");
        map.put("pageSize", pageSize);
        MyHttpRequest.getDefault().postRequest(new TypeToken<LzyResponse<List<PraiseEntity>>>() {
        }.getType(), null, Urls.getMyMessageByType, SignUtils2.makeSignStr(map), false, new ObserverCallback<List<PraiseEntity>>() {
            @Override
            public void onSuccess(List<PraiseEntity> data) {
                if (currentPage == 1) {
                    list.clear();
                    integralAdapter.notifyDataSetChanged();
                }

                if(smrRf==null||llNone==null){
                    return;
                }
                if (data != null && data.size() > 0) {

                    list.addAll(data);
                    integralAdapter.notifyDataSetChanged();
                    smrRf.setVisibility(View.VISIBLE);
                    llNone.setVisibility(View.GONE);
                } else {
                    isEnd = true;
                    if (list.size() == 0) {
                        smrRf.setVisibility(View.GONE);
                        llNone.setVisibility(View.VISIBLE);
                    }
                }
                loadEnd();
            }

            @Override
            public void onError(String errorMsg) {
                loadEnd();
                MyToast.show(Utils.onErrorTips(errorMsg));
            }
        });
    }


    @OnClick(R.id.common_title_back)
    public void onClick() {
        finish();
    }

    class MyIntegralAdapter extends BaseQuickAdapter<PraiseEntity, BaseViewHolder> {
        private Context context;

        public MyIntegralAdapter(int layoutResId, Context context, @io.reactivex.annotations.Nullable List<PraiseEntity> data) {
            super(layoutResId, data);
            this.context = context;
        }

        @Override
        protected void convert(BaseViewHolder helper, PraiseEntity item) {

            TextView tvContent = helper.getView(R.id.tvContent);
            TextView tvTime = helper.getView(R.id.tvTime);
            TextView tvIntegral = helper.getView(R.id.tvIntegral);
            if (0 == messageType) {
                tvIntegral.setVisibility(View.VISIBLE);
            } else {
                tvIntegral.setVisibility(View.GONE);
            }
            tvContent.setText(item.getMessageTitle());
            tvIntegral.setText(item.getMessageContent().replace("积分", ""));
            tvTime.setText(Utils.getAccurateDate(item.getCreateTime()));
        }
    }

    private void loadEnd() {
        if (null != smrRf && smrRf.getState() == RefreshState.Refreshing)
            smrRf.finishRefresh(true);
        if (null != smrRf && smrRf.getState() == RefreshState.Loading)
            smrRf.finishLoadMore(true);
    }

}
